/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gridworld_v5;

/**
 *
 * @author Jonah
 */
public class ActorDirector extends Thread {
    /*implementation comment*/
    ////<editor-fold defaultstate="collapsed" desc="Instanced Variables">
    private GameVariables ActDirGV;
    private Game game;
    private boolean running = true;

    @Override
    public void run() {
        while (running){
            try {
                updateVariables();
                Thread.sleep((long) ActDirGV.DirectorTurnTime);
            } catch (InterruptedException e) {
            }
        }
    }
    //</editor-fold>
    ////<editor-fold defaultstate="collapsed" desc="Constructor">
    public ActorDirector(GameVariables gv, Game game) {
        this.ActDirGV = new GameVariables();
        this.game = game;
    }
    //</editor-fold>
    ////<editor-fold defaultstate="collapsed" desc="Class Methods">
    private void updateVariables(){
        int totalBugs = 0, totalCritters = 0, totalFlowers = 0;
        for (ThreadedActor ta : game.getActors()){
            PossibleStates actorType = ta.getActorType();
            switch (actorType){
                case Bug:
                    totalBugs++;
                    break;
                case Critter:
                    totalCritters++;
                    break;
                case Flower:
                    totalFlowers++;
                    break;
            }
        }
        
        ActDirGV.Bugs = totalBugs;
        ActDirGV.Critters = totalCritters;
        ActDirGV.Flowers = totalFlowers;
//        System.err.println(""+ActDirGV.Bugs+ActDirGV.Critters+ActDirGV.Flowers);
        
        System.err.println("ActDirGV.Bugs"+ActDirGV.Bugs+"game.gv.Bugs");
        if (ActDirGV.Bugs != game.gv.Bugs){
            ActDirGV.Bug_TurnTime = (game.gv.Bug_TurnTime*(ActDirGV.Bugs /game.gv.Bugs));
        }
        System.err.println(
                "ActDirGV.Bug_TurnTime set to " + ActDirGV.Bug_TurnTime
                + " miliseconds."
                );

    }
    
    public long getTimeInterval( PossibleStates actorType){
        switch(actorType){
            case Bug:
                return (long)((Math.random()*0.5) * ActDirGV.Bug_TurnTime);
            default:
                return (long) ActDirGV.InitialTurnTime;
        }
    }
    //</editor-fold>
    ////<editor-fold defaultstate="collapsed" desc="Interface Override Methods">
    //</editor-fold>

}
